﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.IO;

namespace DataGenerators.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class GeneratorsTests
    {
        public GeneratorsTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void NameGenerator_GenerateNextValue()
        {
            string expectedNames;
            string expectedSurnames;
            using (Stream stream = Assembly.GetAssembly(typeof(NameGenerator)).GetManifestResourceStream("DataGenerators.DataFiles.surnames.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    expectedSurnames = reader.ReadToEnd();
                }
            }
            using (Stream stream = Assembly.GetAssembly(typeof(NameGenerator)).GetManifestResourceStream("DataGenerators.DataFiles.femalenames.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    expectedNames = reader.ReadToEnd();
                }
            }
            using (Stream stream = Assembly.GetAssembly(typeof(NameGenerator)).GetManifestResourceStream("DataGenerators.DataFiles.malenames.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    expectedNames += reader.ReadToEnd();
                }
            }

            NameGenerator ng = new NameGenerator();
            ng.Initialize(new Microsoft.Data.Schema.Tools.DataGenerator.GeneratorInit());

            ng.GenerateNextValues();
            string actual = ng.Name;

            foreach (string val in actual.Split(' '))
            {
                Assert.IsTrue(expectedNames.Contains(val) || expectedSurnames.Contains(val));
            }

        }

        [TestMethod]
        public void CountryGenerator_GenerateNextValue()
        {
            string expected;
            string expected1;
            using (Stream stream = Assembly.GetAssembly(typeof(NameGenerator)).GetManifestResourceStream("DataGenerators.DataFiles.countries.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    expected = reader.ReadToEnd();
                }
            }
            using (Stream stream = Assembly.GetAssembly(typeof(NameGenerator)).GetManifestResourceStream("DataGenerators.DataFiles.provinces.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    expected1 = reader.ReadToEnd();
                }
            }

            CountryGenerator ng = new CountryGenerator();
            ng.Initialize(new Microsoft.Data.Schema.Tools.DataGenerator.GeneratorInit());

            ng.GenerateNextValues();
            string actual = string.Format("{0},{1}", ng.Id, ng.CountryName);
            string actual1 = string.Format("{0},{1}", ng.Id, ng.ProvinceName);

            Assert.IsTrue(expected.Contains(actual));
            Assert.IsTrue(expected1.Contains(actual1));
        }
    }
}
